<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="medium"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item prop="qbName">
        <el-input
          v-model="queryParams.qbName"
          placeholder="请输入题库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['question:questionBank:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['question:questionBank:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="6">
        <div class="inner-select">
          <span>是否公开：</span>
          <el-select
            v-model="queryParams.qbShow"
            placeholder="请选择"
            size="small"
            @change="changeQbShow"
            :clearable="true"
          >
            <el-option
              v-for="dict in dict.type.qb_question_bank_show"
              :value="dict.value"
              :label="dict.label"
            ></el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="inner-select">
          <span>题库模式：</span>
          <el-select
            v-model="queryParams.qbPattern"
            placeholder="请选择"
            size="small"
            @change="changeQbPattern"
            :clearable="true"
          >
            <el-option
              v-for="dict in dict.type.qb_question_bank_model"
              :value="dict.value"
              :label="dict.label"
            ></el-option>
          </el-select>
        </div>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="questionBankList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="题库名称" align="center" prop="qbName" :show-overflow-tooltip="true" />
      <el-table-column label="题库描述" align="center" prop="qbDesc" :show-overflow-tooltip="true" />
      <el-table-column label="题库封面" align="center" prop="qbCover" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-image class="qbImg" :src="scope.row.qbCover" :preview-src-list="[scope.row.qbCover]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="是否开放" align="center" prop="qbShow">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="非开放题库只有绑定的用户允许访问" placement="top">
            <dict-tag :options="dict.type.qb_question_bank_show" :value="scope.row.qbShow" />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="题库模式" align="center" prop="qbPattern">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qb_question_bank_model" :value="scope.row.qbPattern" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['question:questionBank:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['question:questionBank:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-data"
            @click="handleBindQuList(scope.row)"
            v-hasPermi="['question:questionBank:list']"
          >题目列表</el-button>
          <el-button
            :disabled="scope.row.qbShow==1"
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="handleBindUserList(scope.row)"
            v-hasPermi="['question:questionBank:list']"
          >用户列表</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改题库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
        <el-form-item label="题库名称" prop="qbName">
          <el-input v-model="form.qbName" placeholder="请输入题库名称" />
        </el-form-item>
        <el-form-item label="题库描述" prop="qbDesc">
          <el-input v-model="form.qbDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="题库封面" prop="qbCover">
          <image-upload v-model="form.qbCover" :fileSize="2" />
        </el-form-item>
        <el-form-item label="是否开放" prop="qbShow">
          <el-switch
            style="display: block"
            v-model="form.qbShow"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="开放"
            inactive-text="不开放"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>

        <el-form-item label="题库模式" prop="qbPattrrn">
          <el-switch
            style="display: block"
            v-model="form.qbPattern"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="背题模式"
            inactive-text="答题模式"
            :active-value="2"
            :inactive-value="1"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 题库已绑定题目列表 -->
    <bind-qu-list
      :getBindQuList="qbBindQuList"
      :removeBindQu="removeQu"
      :bindId="bindQuId"
      :dialogAttr="bindQuDialog"
      @openUnBindList="handleUnBindQuList"
    />
    <!-- 题库未绑定题目列表 -->
    <un-bind-qu-list
      :getUnBindQuList="qbUnBindQuList"
      :addUnBindQu="addQu"
      :unBindId="unBindQuId"
      :dialogAttr="unBindQuDialog"
    />
    <!-- 已绑定用户列表 -->
    <bind-user-list
      :getBindUserList="bindQbUserList"
      :removeBindUser="removeUser"
      :bindId="bindUserId"
      :dialogAttr="bindUserDialog"
      @openUnBindList="handleUnBindUserList"
    />
    <!-- 未绑定用户列表 -->
    <un-bind-user-list
      :getUnBindUserList="unBindQbUserList"
      :addUnBindUser="addUser"
      :unBindId="unBindUserId"
      :dialogAttr="unBindUserDialog"
    />
  </div>
</template>

<script>
import {
  listQuestionBank,
  getQuestionBank,
  delQuestionBank,
  addQuestionBank,
  updateQuestionBank,
  qbBindQuList,
  qbUnBindQuList,
  addQu,
  removeQu,
  bindQbUserList,
  unBindQbUserList,
  removeUser,
  addUser
} from "@/api/question/questionBank";
import BindUserList from "@/components/User/bindUserList.vue";
import UnBindUserList from "@/components/User/unBindUserList.vue";
import BindQuList from "@/components/QuInfo/bindQuList.vue";
import UnBindQuList from "@/components/QuInfo/unBindQuList.vue";
export default {
  name: "QuestionBank",
  dicts: [
    "qb_question_bank_show",
    "qb_question_bank_model",
    "qu_qu_grand",
    "qb_qu_type",
    "sys_user_sex"
  ],
  components: {
    BindUserList,
    UnBindUserList,
    BindQuList,
    UnBindQuList
  },
  data() {
    return {
      // 绑定用户弹框属性
      bindUserDialog: {
        title: "",
        show: false
      },
      // 未绑定用户弹框属性
      unBindUserDialog: {
        title: "",
        show: false
      },
      // 用户id属性
      bindUserId: {
        type: "qbId",
        value: 0
      },
      unBindUserId: {
        type: "qbId",
        value: 0
      },
      // 绑定题库弹框属性
      bindQuDialog: {
        title: "",
        show: false
      },
      // 未绑定题库弹框属性
      unBindQuDialog: {
        title: "",
        show: false
      },
      // 题库id属性
      bindQuId: {
        type: "qbId",
        value: 0
      },
      unBindQuId: {
        type: "qbId",
        value: 0
      },
      // 绑定用户函数
      bindQbUserList,
      removeUser,
      // 未绑定用户函数
      unBindQbUserList,
      addUser,
      // 绑定题库函数
      qbBindQuList,
      removeQu,
      // 未绑定题库函数
      qbUnBindQuList,
      addQu,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 题库表格数据
      questionBankList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qbId: undefined,
        qbName: undefined,
        qbShow: undefined,
        qbPattern: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qbName: [
          { required: true, message: "题库名称不能为空", trigger: "blur" }
        ],
        qbShow: [
          { required: true, message: "题库是否公开不能为空", trigger: "blur" }
        ],
        qbPattern: [
          { required: true, message: "题库模式不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询题库列表 */
    getList() {
      this.loading = true;
      listQuestionBank(this.queryParams).then(response => {
        this.questionBankList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        qbId: undefined,
        qbName: undefined,
        qbShow: 0,
        qbPattern: 0,
        createTime: undefined,
        updateTime: undefined,
        showStatus: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.qbPattern = undefined;
      this.queryParams.qbShow = undefined;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.qbId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加题库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const qbId = row.qbId || this.ids;
      getQuestionBank(qbId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改题库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.qbId != null) {
            updateQuestionBank(this.form)
              .then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addQuestionBank(this.form)
              .then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const qbIds = row.qbId || this.ids;
      this.$modal
        .confirm('是否确认删除题库编号为"' + qbIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delQuestionBank(qbIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索题库是否公开 */
    changeQbShow() {
      this.getList();
    },
    /** 搜索题库模式 */
    changeQbPattern() {
      this.getList();
    },
    /** 打开已绑定题目列表 */
    handleBindQuList(row) {
      this.bindQuDialog.title = "已绑定题目列表!";
      this.bindQuDialog.show = true;
      this.bindQuId.value = row.qbId;
      // qbId
      this.qbId = row.qbId;
    },
    /** 打开未绑定题目列表 */
    handleUnBindQuList() {
      this.unBindQuDialog.title = "未绑定题目列表!";
      this.unBindQuDialog.show = true;
      this.unBindQuId.value = this.qbId;
    },
    /** 打开已绑定用户弹框 */
    handleBindUserList(row) {
      this.bindUserDialog.title = "已绑定用户列表!";
      this.bindUserDialog.show = true;
      this.bindUserId.value = row.qbId;
      // qbId
      this.qbId = row.qbId;
    },
    /** 打开未绑定用户弹窗 */
    handleUnBindUserList() {
      this.unBindUserDialog.title = "未绑定用户列表!";
      this.unBindUserDialog.show = true;
      this.unBindUserId.value = this.qbId;
    }
  }
};
</script>

<style scoped>
.qbImg {
  display: block;
  margin: auto;
  width: 120px;
  height: 60px;
}

.inner-select {
  display: flex;
  justify-content: center;
}
.inner-select span {
  font-size: 16px;
  color: #909399;
  line-height: 32px;
}
</style>